package com.wy.pedometer;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * Time:7/29/21
 * Author:ypf
 * Description:
 */
public class Pedometer extends View {


    private String TAG = "Pedometer";

    /**
     * 画背景的
     */
    private Paint mBgPaint;

    /**
     * 画进度的
     */
    private Paint mSchedulePaint;

    /**
     * 画得分的
     */
    private Paint mScorePaint;

    /**
     * 得分 最大270 最小0
     */
    private int mScore = 0;

    /**
     * 起始分
     */
    private int mStartScore = 0;

    /**
     * 圆弧宽度
     */
    private int mArcWidth = 35;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    invalidate();
                    break;
            }
        }
    };

    public Pedometer(Context context) {
        this(context, null, 0);
    }

    public Pedometer(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public Pedometer(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mBgPaint = new Paint();
        mBgPaint.setColor(Color.BLUE);
        mBgPaint.setStrokeWidth(mArcWidth);
        mBgPaint.setStyle(Paint.Style.STROKE);
        mBgPaint.setAntiAlias(true);
        //这个是设置笔帽的，比如你把铅笔头磨成方的还是圆的（线的两端点是圆头的），或者是尖的（正常情况下画出来就是直线）
        mBgPaint.setStrokeCap(Paint.Cap.ROUND);

        mSchedulePaint = new Paint();
        mSchedulePaint.setColor(Color.RED);
        mSchedulePaint.setStrokeWidth(mArcWidth);
        mSchedulePaint.setStyle(Paint.Style.STROKE);
        mSchedulePaint.setAntiAlias(true);
        //这个是设置笔帽的，比如你把铅笔头磨成方的还是圆的（线的两端点是圆头的），或者是尖的（正常情况下画出来就是直线）
        mSchedulePaint.setStrokeCap(Paint.Cap.ROUND);

        mScorePaint = new Paint();
        mScorePaint.setColor(Color.RED);
        mScorePaint.setTextSize(80);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = getWidth();
        int height = getHeight();

        Log.i(TAG, "width:" + width + ", height:" + height);

        int left = mArcWidth;
        int top = mArcWidth;
        int right = width - mArcWidth;
        int bottom = height - mArcWidth;

        //画一个蓝色背景
        canvas.drawArc(left, top, right, bottom, 135, 270, false, mBgPaint);

        //画进度
        canvas.drawArc(left, top, right, bottom, 135, mStartScore, false, mSchedulePaint);


        int x = width / 2;
        int y = height / 2;


        //分数
        //计算基线的坐标
        Paint.FontMetricsInt fontMetrics = mScorePaint.getFontMetricsInt();
        int fontMetricsLeading = fontMetrics.leading;
        int fontMetricsAscent = fontMetrics.ascent;
        int fontMetricsDescent = fontMetrics.descent;
        int fontMetricsTop = fontMetrics.top;
        int fontMetricsBottom = fontMetrics.bottom;
        Log.i(TAG, "leading:" + fontMetricsLeading + ",ascent:" + fontMetricsAscent + ",descent:" + fontMetricsDescent + ",top:" + fontMetricsTop + ",bottom:" + fontMetricsBottom);
        Log.i(TAG, "width:" + getWidth() + ",height:" + getHeight());

        int dy = (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom;
        int baseLine = getHeight() / 2 + dy;
        Log.i(TAG, "dy:" + dy + ", baseLine:" + baseLine);

        //处理padding
        int paddingLeft = getPaddingLeft();
        int paddingRight = getPaddingRight();
        int paddingTop = getPaddingTop();
        int paddingBottom = getPaddingBottom();

        Log.i(TAG, "paddingLeft:" + paddingLeft + ",paddingRight:" + paddingRight + ",paddingTop:" + paddingTop + ",paddingBottom:" + paddingBottom);

        String startScoreString = String.valueOf(mStartScore);

        //计算文字宽度
        Rect bounds = new Rect();
        mScorePaint.getTextBounds(startScoreString, 0, startScoreString.length(), bounds);
        int widthMeasureSpecSize = bounds.width() / 2;

        //绘制文字
        canvas.drawText(String.valueOf(mStartScore), x - widthMeasureSpecSize, baseLine, mScorePaint);

    }

    public synchronized void setmScore(int mScore) {
        this.mScore = mScore;
        ScoreThread scoreThread = new ScoreThread();
        scoreThread.start();
    }

    /**
     * 得分线程
     */
    private class ScoreThread extends Thread {

        @Override
        public void run() {
            super.run();

            while (mStartScore < mScore) {
                ++mStartScore;
                mHandler.sendEmptyMessage(1);
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }

}